package com.ogawa.projectcommon.viewmodel
import android.content.Context
import androidx.lifecycle.*
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.base.utils.*
import com.ogawa.projectcommon.service.ProjectCommonApiService
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.projectcommon.bean.UpdateRequestBean
import com.ogawa.projectcommon.bean.UpdateResponseBean
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.projectcommon.utils.launch


class CheckUpdateModel : BaseViewModel() {
    val mUpdateResponseBean = MutableLiveData<UpdateResponseBean>()

    fun checkUpdate(context:Context) {
        launch({

            var mUpdateRequestBean = UpdateRequestBean()
            mUpdateRequestBean.phoneid = MacAdressUtils.getMacFromHardware(context)
            mUpdateRequestBean.appversion = AppUtil.getVersionCode(context).toString()
            mUpdateRequestBean.mac = MacAdressUtils.getMacFromHardware(context)
            mUpdateRequestBean.systype = 2
            mUpdateRequestBean.appversion = AppUtil.getVersionCode(context).toString()
            mUpdateRequestBean.sysversion = AppUtil.getSystemVersion()
            mUpdateRequestBean.languagecode = "zh-cn"

            loadState.value = LoadState.Loading("")
            mUpdateResponseBean.value =
                RetrofitManager.INSTANCE.createService(ProjectCommonApiService::class.java)
                    .checkUpdate(mUpdateRequestBean)
                    .dataConvert()



            loadState.value = LoadState.Success("")
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})

    }

}
